package com.eleven.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eleven.pojo.Comments;
import com.eleven.pojo.Msg;
import com.eleven.pojo.QuestionOther;
import com.eleven.service.QuestionOtherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class QuestionOtherController {
	@Autowired
	QuestionOtherService questionOtherService;

	public QuestionOtherService getQuestionOtherService() {
		return questionOtherService;
	}

	public void setQuestionOtherService(QuestionOtherService questionOtherService) {
		this.questionOtherService = questionOtherService;
	}
	/**
	 * 查看某支下的全部问题
	 */
	@RequestMapping(value="/getAllQuestionOtherByQuestionId/{questionId}",method=RequestMethod.GET)
	@ResponseBody
	public Msg getAllQuestionOtherByQuestionId(@RequestParam(value="pn",defaultValue="1")Integer pn,@PathVariable("questionId") Integer questionId) {
		PageHelper.startPage(pn, 10);//传入页码和每页显示的数量
		List<QuestionOther> questionOther = questionOtherService.getAllQuestionOtherByQuestionId(questionId);

		PageInfo pageInfo = new PageInfo(questionOther, 1);//1是连续显示1页的内容
		if(questionOther.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		}else {
			return Msg.fail().add("message", "该分支下暂无问题");
		}	
	}
	/**
	 * 增加问题
	 */
	@RequestMapping(value="/saveQuestionOther",method=RequestMethod.GET)
	@ResponseBody
	public Msg saveQuestionOther(@RequestParam("questionDate")Date questionDate,@RequestParam("questionAnswer")String questionAnswer,@RequestParam("questionotherName")String questionotherName,@RequestParam("questionId")Integer questionId) {
		QuestionOther questionOther=new QuestionOther();
		questionOther.setQuestionId(questionId);
		questionOther.setQuestionotherName(questionotherName);
		questionOther.setQuestionAnswer(questionAnswer);
		questionOther.setQuestionDate(questionDate);
		int num = questionOtherService.saveQuestionOther(questionOther);
		if(num > 0) {
			return Msg.success();
		}else {
			return Msg.fail();
		}
			
	}
	
	/**
	 * 查看全部问题
	 */
	@RequestMapping(value = "/getAllQuestionOther", method = RequestMethod.GET)
	@ResponseBody
	public Msg getAllQuestionOther(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
		PageHelper.startPage(pn, 10);// 引入pageHelpers
		List<QuestionOther> questionOther = questionOtherService.getAllQuestionOther();
		PageInfo pageInfo = new PageInfo(questionOther, 1);
		if (questionOther.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		} else {
			return Msg.fail().add("message", "暂无问题");
		}
	}
	/**
	 * 查看全部问题按时间逆序
	 */
	@RequestMapping(value = "/getAllQuestionOtherTime", method = RequestMethod.GET)
	@ResponseBody
	public Msg getAllQuestionOtherTime(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
		PageHelper.startPage(pn, 10);// 引入pageHelpers
		List<QuestionOther> questionOther = questionOtherService.getAllQuestionOtherTime();
		PageInfo pageInfo = new PageInfo(questionOther, 1);
		if (questionOther.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		} else {
			return Msg.fail().add("message", "暂无问题");
		}
	}
}
